

#import "CaseManageController.h"
#import "QcCaseHeadView.h"
#import "QcAddCaseController.h"

#import "QcManageCell.h"

@interface CaseManageController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;


@end


static NSString *cellad =@"sb";
@implementation CaseManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //解档
    self.manageModel = [QcManageModel unarchiver].mutableCopy;
    
    [self setupUI];
    
}


#pragma mark
#pragma mark - 懒加载模型
-(NSMutableArray<QcManageModel *> *)manageModel {
    
    if (!_manageModel) {
        _manageModel = [NSMutableArray array];
    }
    
    return _manageModel;
}


#pragma mark
#pragma mark - 创建界面

- (void)setupUI {
    
    UITableView *tableView = [[UITableView alloc]init];
    
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
    
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    tableView.estimatedRowHeight = 100;
    
    self.tableview = tableView;
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    [tableView registerNib:[UINib nibWithNibName:@"QcManageCell" bundle:nil] forCellReuseIdentifier:cellad];;
    QcCaseHeadView *caseHeadview = [QcCaseHeadView headViewWithNib];
    
    self.tableview.tableHeaderView = caseHeadview;
    
    
    UIView *footView = [[UIView alloc]init];
    
    footView.backgroundColor = [UIColor colorWithRed:69/255.0 green:178/255.0 blue:175/255.0 alpha:1];
    
    [self.view addSubview:footView];
    
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).mas_offset(10);
        make.right.equalTo(self.view).mas_offset(-10);
        make.bottom.equalTo(self.view).mas_offset(-10);
        
        make.height.mas_offset(50);
    }];
    
    UIButton *button = [[UIButton alloc]init];
    
    [button setTitle:@"添加病例" forState:UIControlStateNormal];
    
    [footView addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.centerX.equalTo(footView);
        
        make.width.equalTo(self.view);
    }];
    
    [button addTarget:self action:@selector(clickAddButton :) forControlEvents:UIControlEventTouchUpInside];
    
    
}

#pragma mark
#pragma mark - BUtton点击事件

- (void)clickAddButton:(UIButton *)button {
    
    QcAddCaseController *add =[[QcAddCaseController alloc] init];
    
    WEAKSELF
    add.modelBlock = ^(QcManageModel* Mmodel){
        
    [weakSelf.manageModel addObject:Mmodel];
        
        //归档
        [QcManageModel archiver:weakSelf.manageModel.copy];
        
        [weakSelf.tableview reloadData];
    };
    
        
    [self.navigationController pushViewController:add animated:YES];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.manageModel.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     QcManageCell *cell =[tableView dequeueReusableCellWithIdentifier:cellad forIndexPath:indexPath];
    
    cell.qcManageModel = self.manageModel[indexPath.row];
    
    return cell;
}






#pragma mark -
#pragma mark - Delegate

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

#pragma mark -
#pragma mark - 删除病历
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //1 删除数据源
        [self.manageModel removeObjectAtIndex:indexPath.row];
        //2 归档
        [QcManageModel archiver:self.manageModel.copy];
        //3 删除单元格
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (NSString*) tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"删除";
}

#pragma mark -
#pragma mark - 选中单元格 修改病历
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QcAddCaseController *addCaseVC = [[QcAddCaseController alloc]initWithCaseRecord:self.manageModel[indexPath.row]];
    WEAKSELF
    addCaseVC.modelBlock = ^(QcManageModel * caseRecord){
        
        [weakSelf.manageModel replaceObjectAtIndex:indexPath.row withObject:caseRecord];
        
        //归档
        [QcManageModel archiver:weakSelf.manageModel.copy];
        
        
        [weakSelf.tableview reloadData];
    };
    
    [self.navigationController pushViewController:addCaseVC animated:YES];
    
    
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)dealloc {
    
  //  NSLog(@"销毁了");
}

@end
